<!--
 * @Author: HeAo
 * @Date: 2021-10-14 11:25:38
 * @LastEditTime: 2021-10-14 11:46:46
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2105\3-2-Vue\day06\3-vuex模块化开发.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/vue.js"></script>
  <script src="../js/vuex.js"></script>
</head>

<body>
  <div id="app">
    <h3>moduleA:{{msg1}}-{{upperMsg1}}
      <button @click="SET_MSG1('模块A数据')">修改A模块中的msg</button>
    </h3>
    <h3>moduleB:{{msg2}}-{{upperMsg2}}</h3>
  </div>
  <script>
    let moduleA = {
      namespaced: true,
      state() {
        return {
          msg1: 'moduleA data'
        }
      },
      getters: {
        upperMsg1(state) {
          return state.msg1.toUpperCase()
        }
      },
      mutations: {
        SET_MSG1(state, payload) {
          state.msg1 = payload
        }
      },
      actions: {

      }
    }
    let moduleB = {
      // namespaced表示命名空间，开启之后，组件就可以通过命名空间访问子模块仓库
      namespaced: true,
      state() {
        return {
          msg2: 'moduleB data'
        }
      },
      getters: {
        upperMsg2(state) {
          return state.msg2.toUpperCase()
        }
      },
      mutations: {
        SET_MSG2(state, payload) {
          state.msg2 = payload
        }
      },
      actions: {

      }
    }

    let store = new Vuex.Store({
      // 使用modules关键字，将子模块仓库，注入到store中央仓库中
      modules: {
        a: moduleA,
        b: moduleB
      }
    })

    // 解构辅助函数
    let { mapState, mapGetters, mapMutations, mapActions } = Vuex

    let vm = new Vue({
      el: '#app',
      data: {},
      methods: {
        // 使用辅助函数，映射状态机中的突变或者动作
        ...mapMutations('a', ['SET_MSG1']),
        ...mapMutations('b', ['SET_MSG2']),
      },
      computed: {
        // 使用辅助函数，映射状态机中的状态
        ...mapState('a', ['msg1']),
        ...mapState('b', ['msg2']),
        ...mapGetters('a', ['upperMsg1']),
        ...mapGetters('b', ['upperMsg2']),
      },
      created() {

      },
      store: store
    })
  </script>
</body>

</html>